require "common/Define"
require "framework/BaseController"
require "FairyGUI"
require "Card"

TurnCardMain = BaseController:New({mControllerName = "TurnCardMain"});

function TurnCardMain.Main()
	-- body
	TurnCardMain:Awake();
end

function TurnCardMain:Awake()
	Application.targetFrameRate = 60;
	Stage.inst.onKeyDown:Add(self.OnKeyDown, self);

	-- attack pacakge, package in resource or assetbundle
	local packageName = "UI/TurnCard";
	UIPackage.AddPackage(packageName);
	fgui.register_extension("ui://TurnCard/CardComponent", Card);

	local componentName = "Main";
	self.mainComponent = UIPackage.CreateObject("TurnCard", componentName).asCom;
	GRoot.inst:AddChild( self.mainComponent );

	--local go = GameObject();
	--local panel = go:AddComponent(typeof(UIPanel));
	--panel.container:AddChild(self.mainComponent);
	--GRoot.inst:AddChild( panel );

	-- self.mainComponent:SetSize(GRoot.inst.width, GRoot.inst.height);
	-- self.mainComponent:AddRelation(GRoot.inst, RelationType.Size);
	GRoot.inst:SetContentScaleFactor(1134, 640, FairyGUI.UIContentScaler.ScreenMatchMode.MatchWidthOrHeight);

	self:OnViewCreated(self.mainComponent);
end

function TurnCardMain:InitView()
	self._c0 = self.mainComponent:GetChild("c0");
	self._c0.tname = "card1";

	self._c1 = self.mainComponent:GetChild("c1");
	self._c1:SetPerspective();
	self._c1.tname = "card2";

	print(self._c0, self._c1);

	self._c0.onClick:Add(self.__clickCard, self);
	self._c1.onClick:Add(self.__clickCard, self);
end

function TurnCardMain:__clickCard(context)
	local card = context.sender;
	print("click_card: ", context.sender, card.tname);
	card:Turn();
end

function TurnCardMain:OnKeyDown(context)
 	-- body
 	if (context.inputEvent.keyCode == KeyCode.Escape) then
		print "Exit Game!"
		Application.Quit();
	end
end

